package com.itouch.bracelet.diseaseinfo;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itouch.bracelet.R;
import com.itouch.bracelet.bean.DiseaseInfo;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class DiseaseInfoAdapter extends BaseAdapter{

	private ArrayList<DiseaseInfo> mData;
	LayoutInflater inflater;
	
	public DiseaseInfoAdapter(Context context, ArrayList<DiseaseInfo> data)
	{
		if(data == null)
			mData = new ArrayList<DiseaseInfo>();
		else
			mData = data;
		
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void refrashDataAndView(ArrayList<DiseaseInfo> dataList)
	{
		if(dataList != null)
		{
			mData = dataList;
			notifyDataSetChanged();
		}
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mData.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		DiseaseInfo diseaseInfo = mData.get(arg0);
		MyViewHolder holder;
		if(arg1 == null)
		{
			holder = new MyViewHolder();
			arg1 = inflater.inflate(R.layout.diseaseinfo_item, null);
			holder.image = (ImageView) arg1.findViewById(R.id.diseaseinfo_image);
			holder.textView = (TextView) arg1.findViewById(R.id.diseaseinfo_name);
			arg1.setTag(holder);
		}
		else
		{
			holder = (MyViewHolder) arg1.getTag();
		}
		
		if(diseaseInfo.getPath() != null)
		{
			DisplayImageOptions options = new DisplayImageOptions.Builder()
	        .showStubImage(R.drawable.imageunshow).cacheInMemory().build();// 设置图片在下载期间显示的图片
			ImageLoader.getInstance().displayImage(diseaseInfo.getPath(), holder.image, options);
		}
		else
		{
			holder.image.setImageResource(R.drawable.imageunshow);
		}
		holder.textView.setText(diseaseInfo.getName());
		
		return arg1;
	}

	public class MyViewHolder
	{
		private ImageView image;
		private TextView textView;
	}
	
}
